package com.care.controller.admin.sys;

import com.care.controller.ServerUploadUtil;
import com.care.controller.vo.JsonResult;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.multipart.MultipartFile;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * 后台管理api
 * @author xujinnan
 *
 */
@RequestMapping("/admin")
@RestController
public class AdminUploadApi {

    @Resource
    ServerUploadUtil serverUploadUtil;

    @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request) {
        //转换日期格式
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //注册自定义的编辑器
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    @RequestMapping("uploadPic")
    public JsonResult savePicture(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        JsonResult ret = JsonResult.success("", "OK");
        //保存文件
        if(file != null && file.getSize() > 0) {
            //保存文件
            System.out.println("上传的文件名："+file.getOriginalFilename());
            String uuid = UUID.randomUUID().toString();
            String folder = request.getSession().getServletContext().getRealPath("/upload")+File.separator;
            System.out.println("存储的文件路径："+folder);
            String filePath = folder+uuid+file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            System.out.println(filePath);
            InputStream is = null;// 附件输入流
            DataOutputStream out = null;
            try {
                out = new DataOutputStream(new FileOutputStream(filePath));// 存放文件的绝对路径
                is = file.getInputStream();
                byte[] b = new byte[is.available()];
                is.read(b);
                out.write(b);
                File tmpFile = new File(filePath);
                String picUrl = serverUploadUtil.uploadToServer(file.getOriginalFilename(), tmpFile);
                ret.setData(picUrl);
                if(tmpFile.delete()) {
                    System.out.println("临时文件已被删除！");
                }
            } catch (IOException e) {
                e.printStackTrace();
                return JsonResult.error("图片上传失败", "ERROR");
            } finally {
                if (is != null) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return ret;
    }
}
